package cn.dnui_yxk801.dnuifood_16110100801.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ShopBean;
import cn.dnui_yxk801.dnuifood_16110100801.controller.FoodListActivity;


public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //public static String shopid;
    private Context context;
    private List<ShopBean> datalist;
    private LayoutInflater layoutInflater;
    private static String Baseurl="http://172.24.10.175:8080/foodService/";

    public ShopAdapter(Context context,List datalist)
    {
        this.context=context;
        this.datalist=datalist;
        layoutInflater=LayoutInflater.from(context);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=layoutInflater.from(viewGroup.getContext()).inflate(R.layout.shopitem,viewGroup,false);
       // v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int i) {
        ShopBean entity=(ShopBean)datalist.get(i);
        if (entity==null)
            return;
      //  shopid=entity.getShop_id();
        ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(entity.getLevel());
        viewHolder.res_tel.setText(entity.getPhonenum());
        Picasso.with(context).load(Baseurl+entity.getPic()).into(viewHolder.res_pic);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"菜谱",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent( context,FoodListActivity.class);
                String shopid=datalist.get(i).getShop_id();
                String shopname=datalist.get(i).getShopname();
                intent.putExtra("shopid",shopid);
                intent.putExtra("shopname",shopname);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

//    @Override
//    public void onClick(View view) {
//        Toast.makeText(context,"测试",Toast.LENGTH_SHORT).show();
//        Intent i=new Intent( context,test.class);
//        context.startActivity(i);
//    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        RatingBar res_bar;
        TextView res_name,res_address,res_tel;
        ImageView res_pic;


        public ViewHolder(View view) {
            super(view);
            res_name=(TextView)view.findViewById(R.id.res_name);
            res_bar=(RatingBar)view.findViewById(R.id.ratingBar);
            res_address=(TextView)view.findViewById(R.id.res_address);
            res_pic=(ImageView)view.findViewById(R.id.imageView);
            res_tel=(TextView)view.findViewById(R.id.res_tel);

        }
    }
}
